package com.xxxx.seckill.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xxxx.seckill.mapper.GoodsMapper;
import com.xxxx.seckill.pojo.Goods;
import com.xxxx.seckill.pojo.dto.GoodsDto;
import com.xxxx.seckill.service.IGoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * <p>
 * 商品表 服务实现类
 * </p>
 *
 * @author zhoubin
 * @since 2022-08-20
 */
@Service
public class GoodsServiceImpl extends ServiceImpl<GoodsMapper, Goods> implements IGoodsService {

    /**
     * 查询所有商品
     */
    @Autowired
    private GoodsMapper goodsMapper;
    @Autowired
    private RedisTemplate redisTemplate;

    @Override
    public List<GoodsDto> getGoodsDto() {
        List<GoodsDto> goodsAll = goodsMapper.getGoodsAll();
        return goodsAll;
    }

    /**
     * 查询指定商品详情
     * @param id
     * @return
     */
    @Override
    public GoodsDto getDetail(int id) {
        GoodsDto detail = goodsMapper.getDetail(id);
        // 把库存数量放入缓存
        redisTemplate.opsForValue().set("count:"+id,detail.getStockCount(),6, TimeUnit.HOURS);
        return detail;
    }
}
